package com.jhclothing.inbound.service.impl;

import com.jhclothing.common.enums.ClothingSize;
import com.jhclothing.inbound.domain.ClothingInbound;
import com.jhclothing.inbound.mapper.ClothingInboundMapper;
import com.jhclothing.inbound.service.IClothingInboundService;
import com.jhclothing.product.domain.ClothingInventory;
import com.jhclothing.product.domain.ClothingType;
import com.jhclothing.product.service.IClothingInventoryService;
import com.jhclothing.product.service.IClothingTypeService;
import com.jhclothing.product.vo.ClothingInventoryVo;
import com.jhclothing.product.vo.StatisticInOutVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 服饰入库Service业务层处理
 *
 * @author zyp
 * @date 2024-04-25
 */
@Service
public class ClothingInboundServiceImpl implements IClothingInboundService {
    @Autowired
    private ClothingInboundMapper clothingInboundMapper;
    @Autowired
    private IClothingInventoryService clothingInventoryService;
    @Autowired
    private IClothingTypeService clothingTypeService;

    /**
     * 查询服饰入库
     *
     * @param inboundId 服饰入库主键
     * @return 服饰入库
     */
    @Override
    public ClothingInbound selectClothingInboundByInboundId(Long inboundId) {
        return clothingInboundMapper.selectClothingInboundByInboundId(inboundId);
    }

    /**
     * 查询服饰入库列表
     *
     * @param clothingInbound 服饰入库
     * @return 服饰入库
     */
    @Override
    public List<ClothingInbound> selectClothingInboundList(ClothingInbound clothingInbound) {
        return clothingInboundMapper.selectClothingInboundList(clothingInbound);
    }

    /**
     * 统计一定时间内的服饰入库数量
     */
    @Override
    public List<StatisticInOutVo> statisticClothingInboundNum(StatisticInOutVo statisticInOutVo) {
        return clothingInboundMapper.statisticClothingInboundNum(statisticInOutVo);
    }

    /**
     * 新增服饰入库
     *
     * @param clothingInbound 服饰入库
     * @return 结果
     */
    @Override
    @Transactional
    public String insertClothingInbound(ClothingInbound clothingInbound) {
        ClothingInventory clothing = clothingInventoryService.selectClothingInventoryByClothingTypeAndSize(
                clothingInbound.getClothingTypeId(), clothingInbound.getClothingSize());
        if (null == clothing) {
            ClothingType clothingType = clothingTypeService.selectClothingTypeByClothingTypeId(clothingInbound.getClothingTypeId());
            String clothingSize = ClothingSize.getClothingSizeNameByCode(clothingInbound.getClothingSize());
            return String.format("当前选择的服饰品类: %s，服饰尺码为： %s 对应的服饰不存在", clothingType.getClothingTypeCodeAndSex(), clothingSize);
        }

        clothingInbound.setClothingInventoryId(clothing.getClothingInventoryId());
        Integer clothingInventory = clothing.getNowInventory() + clothingInbound.getInboundNum();
        clothingInventoryService.updateClothingInventory(clothing.getClothingInventoryId(), clothingInventory, clothingInbound.getUpdateBy());
        clothingInboundMapper.insertClothingInbound(clothingInbound);
        return "SUCCESS";
    }

    /**
     * 修改服饰入库
     *
     * @param clothingInbound 服饰入库信息
     * @return 结果
     */
    @Override
    @Transactional
    public String updateClothingInbound(ClothingInbound clothingInbound) {
        // 查找要入库的服饰品类
        ClothingInventory currentClothing = clothingInventoryService.selectClothingInventoryByClothingTypeAndSize(
                clothingInbound.getClothingTypeId(), clothingInbound.getClothingSize());
        if (null == currentClothing) {
            ClothingType clothingType = clothingTypeService.selectClothingTypeByClothingTypeId(clothingInbound.getClothingTypeId());
            String clothingSize = ClothingSize.getClothingSizeNameByCode(clothingInbound.getClothingSize());
            return String.format("当前选择的服饰品类: %s，服饰尺码为： %s 对应的服饰不存在", clothingType.getClothingTypeCodeAndSex(), clothingSize);
        }
        // 上次入库信息
        ClothingInbound originInbound = this.selectClothingInboundByInboundId(clothingInbound.getInboundId());

        // 本次入库服饰类别与之前记录一致
        if (currentClothing.getClothingInventoryId() == originInbound.getClothingInventoryId()) {
            int clothingInventoryNum = currentClothing.getNowInventory() + clothingInbound.getInboundNum() - originInbound.getInboundNum();
            if (clothingInventoryNum < 0) {
                String clothingSize = ClothingSize.getClothingSizeNameByCode(clothingInbound.getClothingSize());
                return String.format("修改服饰品类为%s, 服饰尺码为%s 产品的入库数量后，服饰库存为负数，请重新选择入库数量",
                        currentClothing.getClothingType().getClothingTypeCodeAndSex(), clothingSize);
            }
            clothingInventoryService.updateClothingInventory(currentClothing.getClothingInventoryId(), clothingInventoryNum, clothingInbound.getUpdateBy());
        } else { // 本次入库服饰类别与之前记录不一致
            ClothingInventory originClothing = clothingInventoryService.selectClothingInventoryByClothingInventoryId(originInbound.getClothingInventoryId());
            int originInventoryNum = originClothing.getNowInventory() - originInbound.getInboundNum();
            if (originInventoryNum < 0) {
                String clothingSize = ClothingSize.getClothingSizeNameByCode(originClothing.getClothingSize());
                return String.format("修改入库的服饰品类或服饰尺码后，原服饰品类为 %s，服饰尺码为 %s 的产品库存为负数，请先确认原服饰的库存数量",
                        originClothing.getClothingType().getClothingTypeCodeAndSex(), clothingSize);
            }
            clothingInventoryService.updateClothingInventory(originClothing.getClothingInventoryId(), originInventoryNum, clothingInbound.getUpdateBy());
            clothingInventoryService.updateClothingInventory(currentClothing.getClothingInventoryId(),
                    currentClothing.getNowInventory() + clothingInbound.getInboundNum(), clothingInbound.getUpdateBy());
        }
        clothingInbound.setClothingInventoryId(currentClothing.getClothingInventoryId());
        clothingInboundMapper.updateClothingInbound(clothingInbound);
        return "SUCCESS";
    }

    /**
     * 批量删除服饰入库
     *
     * @param inboundIds 需要删除的服饰入库主键
     * @param updateBy   更新者
     * @return 结果
     */
    @Override
    @Transactional
    public String deleteClothingInboundByInboundIds(Long[] inboundIds, String updateBy) {
        List<ClothingInventoryVo> clothingInventoryVoList = new ArrayList<>();
        for (Long inboundId : inboundIds) {
            ClothingInbound clothingInbound = this.selectClothingInboundByInboundId(inboundId);
            ClothingInventory clothing = clothingInventoryService.selectClothingInventoryByClothingInventoryId(clothingInbound.getClothingInventoryId());
            int clothingInventory = clothing.getNowInventory() - clothingInbound.getInboundNum();
            if (clothingInventory < 0) {
                String clothingSize = ClothingSize.getClothingSizeNameByCode(clothing.getClothingSize());
                return String.format("删除当前入库信息后，服饰品类为%s, 服饰尺码为%s 的产品库存为负数，无法删除当前入库信息",
                        clothing.getClothingType().getClothingTypeCodeAndSex(), clothingSize);
            }
            clothingInventoryVoList.add(new ClothingInventoryVo(clothingInbound.getClothingInventoryId(), clothingInventory, updateBy));
        }

        for (ClothingInventoryVo clothingInventoryVo : clothingInventoryVoList) {
            clothingInventoryService.updateClothingInventory(clothingInventoryVo.getClothingInventoryId(),
                    clothingInventoryVo.getClothingInventory(), clothingInventoryVo.getUpdateBy());
        }
        clothingInboundMapper.deleteClothingInboundByInboundIds(inboundIds);
        return "SUCCESS";
    }
}
